package com.manage.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manage.mapper.CollectionsListMapper;
import com.manage.mapper.CollectionsMapper;
import com.manage.pojo.Collections;
import com.manage.pojo.operation.CollectionsList;
import com.manage.utils.PagedResult;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.List;

@Service
public class CollectionsServiceImpl implements CollectionsService{

    @Autowired
    private CollectionsMapper collectionsMapper;
    @Autowired
    private CollectionsListMapper collectionsListMapper;
    @Autowired
    private Sid cid;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryCollectionsIsExit(String collectionId) {
        Collections collection = new Collections();
        collection.setCollectionId(collectionId);
        Collections result = collectionsMapper.selectOne(collection);
        return result == null ? false : true;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void saveCollections(Collections collection) {
        String collectionId = cid.nextShort();
        collection.setCollectionId(collectionId);
        collectionsMapper.insert(collection);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void collectionDelete(String collectionId) {
        Example collectionExample = new Example(Collections.class);
        Criteria criteria = collectionExample.createCriteria();
        criteria.andEqualTo("collectionId", collectionId);
        collectionsMapper.deleteByExample(collectionExample);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public CollectionsList queryCollectionsInfoByCollectionId(String collectionId) {
        CollectionsList result = collectionsListMapper.queryCollectionsByCollectionsId(collectionId);
        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult queryCollectionsInfoByUserId(Integer page, Integer pageSize, String userId) {
        PageHelper.startPage(page, pageSize);
        List<CollectionsList> list= collectionsListMapper.queryCollectionsByUserId(userId);
        PageInfo<CollectionsList> pageList = new PageInfo<>(list);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult queryCollectionsInfoByShoesId(Integer page, Integer pageSize, String shoesId) {
        PageHelper.startPage(page, pageSize);
        List<CollectionsList> list= collectionsListMapper.queryCollectionsByShoesId(shoesId);
        PageInfo<CollectionsList> pageList = new PageInfo<>(list);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult queryCollections(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<CollectionsList> list= collectionsListMapper.queryAllCollections();
        PageInfo<CollectionsList> pageList = new PageInfo<>(list);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }
}